<?php

namespace app\admin\controller;

use app\BaseController;
use app\app\model\ProjectGFWJModel;
use app\Request;


class ProjectGfwj extends BaseController
{
    public function list()
    {
        $wj_name = input('wj_name','');
        $fw_time = input('fw_time','');
        $key_word = input('key_word','');
        $page_size = input('pageSize', '20');

        $where = [];
        if ($wj_name) {
            $where[] = ['wj_name', 'like', '%' . $wj_name . '%'];
        }
        if ($fw_time) {
            $where[] = ['fw_time', '=', $fw_time];
        }
        if ($key_word) {
            $where[] = ['key_word', 'like', '%' . $key_word . '%'];
        }

        $list = ProjectGFWJModel::alias('a')
            ->field('a.*,b.file_name,b.file_path,b.use_count')
            ->leftjoin('sys_file_info b', 'b.file_id = a.file_id')
            ->where($where)->paginate([
                'list_rows'=> $page_size,
                'var_page' => 'pageNum',
            ]);
        $this->success('success', $list);
    }

    public function add(Request $request)
    {
        $data = $request->param();
        $model = new ProjectGFWJModel();
        $result = $model->save($data);
        if ($result !== false) {
            $this->success('添加成功!');
        } else {
            $this->error("保存失败！");
        }
    }

    public function update(Request $request)
    {
        $data = $request->param();
        $id = $request->param('id', 0, 'intval');
        $result = ProjectGFWJModel::strict(false)->where('id', $id)->update($data);
        if ($result !== false) {
            $this->success("保存成功！");
        } else {
            $this->error("保存失败！");
        }
    }


    public function delete(Request $request)
    {
        $data = $request->param();
        $ids = $data['ids'];
        ProjectGFWJModel::destroy(function($query) use ($ids){
            $query->where('id', 'in', $ids);
        });
    }
}
